<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="80px">
      <el-row>
        <el-form-item label="会员名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入会员名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <!-- <el-form-item label="状态" prop="dataStatus">
          <el-select
            v-model="queryParams.dataStatus"
            placeholder="请选择状态"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item> -->

        <el-form-item style="margin-left: 22px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <!-- <el-row class="row-mbinfo">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:brand:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:brand:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:brand:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->
    <div class="divTable" style="margin-top: 20px">
      <el-table border v-loading="loading" :data="brandList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="会员名称" align="center" prop="customerName" />
        <el-table-column label="签到日期" align="center" prop="signDate" />
        <el-table-column label="签到天数" align="center" prop="theDay" />
        <el-table-column label="是否补签" align="center" prop="isRepair">
          <template slot-scope="scope">
            <span>{{ scope.row.isRepair == 0 ? "否" : "是" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="奖励菠萝派" align="center" prop="rewardScore" />
        <el-table-column label="奖励服务项目" align="center" prop="serviceItemName" />
        <el-table-column label="奖励套餐卡" align="center" prop="cardName" />
        <el-table-column label="奖励优惠券" align="center" prop="couponName" />
        <el-table-column label="补签消耗菠萝派" align="center" prop="repairGoldCoin" />
		<el-table-column label="签到时间" align="center" prop="createTime" />

        <!-- <el-table-column label="数据状态" align="center" prop="dataStatus">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column> -->

        <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="toOpenSetRule(scope.row)"
            >新增规则</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['device:brand:edit']"
              >修改</el-button
            >
            <el-button
              v-if="scope.row.dataStatus == '0'"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['device:brand:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <!-- 添加或修改签到/系列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="会员名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入会员名称" class="select-normal" maxlength="20"
            show-word-limit />
        </el-form-item>

        <el-form-item label="签到图标">
          <imageArraySize :imageStr.sync="form.signIcon" :key="form.signIcon" :width="130" :height="100"
            :msg="'图片要求：宽750px高563px，大小8M内。'" :size="1024" :limit="1" ref="childs"></imageArraySize>
        </el-form-item>

        <el-form-item label="签到说明" prop="signDesc">
          <el-input v-model="form.signDesc" placeholder="请输入签到说明" style="width: 400px" maxlength="100"
            show-word-limit />
        </el-form-item>

        <el-form-item label="是否开启补签" prop="isLock">
          <el-select v-model="form.canRepair" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="规则日期" prop="startDate">
          <el-col :span="12">
            <div>
              <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="getTime" type="datetimerange"
                range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions0"
                @blur="timeAssignment">
              </el-date-picker>
            </div>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBrand,
  getBrand,
  delBrand,
  addBrand,
  updateBrand,
  updateBrandStatus,
} from "@/api/service/signDetail";
import imageArraySize from "@/components/ImageUpload/imageArraySize";
export default {
  name: "Brand",
  components: {
    imageArraySize,
  },
  data() {
    return {
      getTime: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中数组的名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 签到/系列表格数据
      brandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用于类别关联的分类显示
      dialogVisible: false,
      //常用状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        dataStatus: null,
      },

      pickerOptions0: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },

      options: [
        {
          value: "1",
          label: "是",
        },
        {
          value: "0",
          label: "否",
        },
      ],
      optionsA: [
        {
          value: "1",
          label: "能量",
        },
        {
          value: "0",
          label: "非能量",
        },
      ],
      // 表单参数
      form: {
        dialogImageUrl: "",
        dataPriority: "",
      },
      // 表单校验
      rules: {
        customerName: [
          {
            required: true,
            message: "会员名称不能为空",
            trigger: "blur",
          },
        ],
        dataPriority: [
          {
            required: true,
            message: "排序不能为空",
            trigger: "blur",
          },
        ],
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_data_status").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    dataPriorityInput(value) {
      let js = value + "";
      return js.replace(/^(0+)|[^\d]+/g, "");
    },

    toOpenSetRule(row) {
      this.dialogVisible = true
      this.viewRuleRow = row
    },

    /** 查询签到/系列列表 */
    getList() {
      this.loading = true;
      listBrand(this.queryParams).then((response) => {
        this.brandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleRemove(file, fileList) { },
    handlePictureCardPreviewLogo(file) {
      this.logoUr = file.url;
      this.dialogVisible = true;
    },
    handlePictureCardPreviewImg(file) {
      this.signIcon = file.url;
      this.dialogVisible = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 关联的取消按钮
    cancelBrand() {
      this.openBrand = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        customerName: null,
        logoUrl: null,
        signDesc: null,
        signIcon: null,

        dataPriority: 10,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    timeAssignment(row) {
      this.form.startDate = this.getTime[0];
      this.form.endDate = this.getTime[1];
    },

    /** 签到菜单 */
    getMenuTreeselect() {
      menuTreeselect().then((response) => {
        this.menuOptions = response.data;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.recordId);
      this.names = selection.map((item) => item.customerName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      //用于禁用删除
      if (this.multiple == false) {
        for (var i = 0; i < selection.length; i++) {
          var element = selection[i];
          if ("1" === element.dataStatus) {
            this.multiple = true;
            break;
          }
        }
      }
    },
    // 状态修改
    handleStatusChange(row) {
      let text = row.dataStatus === "0" ? "无效" : "有效";
      this.$confirm(
        '确定要把会员名称为"' + row.customerName + '"设为' + text + "吗？",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return updateBrandStatus(row.recordId, row.dataStatus);
        })
        .then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        })
        .catch(function () {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.getTime = [];
      this.title = "添加签到";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      const recordId = row.recordId || this.ids;
      getBrand(recordId).then((response) => {
        this.form = response.data;
        // this.getTime[0] = this.form.startDate;
        // this.getTime[1] = this.form.endDate;
        this.getTime.push(this.form.startDate);
        this.getTime.push(this.form.endDate);

        console.log(this.form);
        this.open = true;
        this.title = "修改签到";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.childs.selectImgs();
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.recordId != null) {
            updateBrand(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBrand(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const signIds = row.recordId || this.ids;
      const names = row.customerName || this.names;
      this.$confirm('是否确认删除会员名称为"' + names + '"的签到?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delBrand(signIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "device/brand/export",
        {
          ...this.queryParams,
        },
        `商品签到.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.treeOne {
  display: inline-block;
  margin-right: 200px;
}

.treeTwo {
  display: inline-block;
}

.image-rows {
  width: 80px;
  height: auto;
}
</style>
